#!/usr/bin/env bash

if [ "$GRAALVM_HOME" != "" ]
then
    export JAVA_HOME=$GRAALVM_HOME
    export PATH=$GRAALVM_HOME/bin:$PATH
fi

java -version

set -eo pipefail

if [ "$BABASHKA_LEAN" = "true" ]
then
    # Remove all the default features, unless explicitly set to true:
    export BABASHKA_FEATURE_XML="${BABASHKA_FEATURE_XML:-false}"
    export BABASHKA_FEATURE_YAML="${BABASHKA_FEATURE_YAML:-false}"
    export BABASHKA_FEATURE_CSV="${BABASHKA_FEATURE_CSV:-false}"
    export BABASHKA_FEATURE_TRANSIT="${BABASHKA_FEATURE_TRANSIT:-false}"
    export BABASHKA_FEATURE_JAVA_TIME="${BABASHKA_FEATURE_JAVA_TIME:-false}"
    export BABASHKA_FEATURE_JAVA_NET_HTTP="${BABASHKA_FEATURE_JAVA_NET_HTTP:-false}"
    export BABASHKA_FEATURE_JAVA_NIO="${BABASHKA_FEATURE_JAVA_NIO:-false}"
    export BABASHKA_FEATURE_HTTPKIT_CLIENT="${BABASHKA_FEATURE_HTTPKIT_CLIENT:-false}"
    export BABASHKA_FEATURE_HTTPKIT_SERVER="${BABASHKA_FEATURE_HTTPKIT_SERVER:-false}"
    export BABASHKA_FEATURE_CORE_MATCH="${BABASHKA_FEATURE_CORE_MATCH:-false}"
    export BABASHKA_FEATURE_LOGGING="${BABASHKA_FEATURE_LOGGING:-false}"
fi

BABASHKA_LEIN_PROFILES="+uberjar"

if [ "$BABASHKA_FEATURE_JDBC" = "true" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/jdbc"
else
    BABASHKA_LEIN_PROFILES+=",-feature/jdbc"
fi

if [ "$BABASHKA_FEATURE_SQLITE" = "true" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/sqlite"
else
    BABASHKA_LEIN_PROFILES+=",-feature/sqlite"
fi

if [ "$BABASHKA_FEATURE_POSTGRESQL" = "true" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/postgresql"
else
    BABASHKA_LEIN_PROFILES+=",-feature/postgresql"
fi

if [ "$BABASHKA_FEATURE_HSQLDB" = "true" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/hsqldb"
else
    BABASHKA_LEIN_PROFILES+=",-feature/hsqldb"
fi

if [ "$BABASHKA_FEATURE_ORACLEDB" = "true" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/oracledb"
else
    BABASHKA_LEIN_PROFILES+=",-feature/oracledb"
fi

if [ "$BABASHKA_FEATURE_XML" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/xml"
else
    BABASHKA_LEIN_PROFILES+=",-feature/xml"
fi

if [ "$BABASHKA_FEATURE_YAML" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/yaml"
else
    BABASHKA_LEIN_PROFILES+=",-feature/yaml"
fi

if [ "$BABASHKA_FEATURE_CSV" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/csv"
else
    BABASHKA_LEIN_PROFILES+=",-feature/csv"
fi

if [ "$BABASHKA_FEATURE_TRANSIT" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/transit"
else
    BABASHKA_LEIN_PROFILES+=",-feature/transit"
fi

if [ "$BABASHKA_FEATURE_DATASCRIPT" = "true" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/datascript"
else
    BABASHKA_LEIN_PROFILES+=",-feature/datascript"
fi

if [ "$BABASHKA_FEATURE_HTTPKIT_CLIENT" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/httpkit-client"
else
    BABASHKA_LEIN_PROFILES+=",-feature/httpkit-client"
fi

if [ "$BABASHKA_FEATURE_HTTPKIT_SERVER" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/httpkit-server"
else
    BABASHKA_LEIN_PROFILES+=",-feature/httpkit-server"
fi

if [ "$BABASHKA_FEATURE_LANTERNA" = "true" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/lanterna"
else
    BABASHKA_LEIN_PROFILES+=",-feature/lanterna"
fi

if [ "$BABASHKA_FEATURE_CORE_MATCH" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/core-match"
else
    BABASHKA_LEIN_PROFILES+=",-feature/core-match"
fi

if [ "$BABASHKA_FEATURE_HICCUP" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/hiccup"
else
    BABASHKA_LEIN_PROFILES+=",-feature/hiccup"
fi

if [ "$BABASHKA_FEATURE_TEST_CHECK" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/test-check"
else
    BABASHKA_LEIN_PROFILES+=",-feature/test-check"
fi

if [ "$BABASHKA_FEATURE_SPEC_ALPHA" = "true" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/spec-alpha"
else
    BABASHKA_LEIN_PROFILES+=",-feature/spec-alpha"
fi

if [ "$BABASHKA_FEATURE_SELMER" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/selmer"
else
    BABASHKA_LEIN_PROFILES+=",-feature/selmer"
fi

if [ "$BABASHKA_FEATURE_LOGGING" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/logging"
else
    BABASHKA_LEIN_PROFILES+=",-feature/logging"
fi

if [ "$BABASHKA_FEATURE_PRIORITY_MAP" != "false" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/priority-map"
else
    BABASHKA_LEIN_PROFILES+=",-feature/priority-map"
fi

if [ "$BABASHKA_FEATURE_RRB_VECTOR" = "true" ]
then
    BABASHKA_LEIN_PROFILES+=",+feature/rrb-vector"
else
    BABASHKA_LEIN_PROFILES+=",-feature/rrb-vector"
fi

#lein with-profiles "$BABASHKA_LEIN_PROFILES,+reflection,-uberjar" deps :tree
#exit 0

mkdir -p resources/META-INF/babashka
cp deps.edn resources/META-INF/babashka/deps.edn

rm -rf resources/*.class
# SVM_JAR=$(find -L "$GRAALVM_HOME" | grep svm.jar)
# "$GRAALVM_HOME/bin/javac" -cp "$SVM_JAR" resources/UrlClassLoaderSubstitutions.java
# "$GRAALVM_HOME/bin/javac" -cp "$SVM_JAR" resources/CutOffSunAwtWwwContentAudioAiff.java
# "$GRAALVM_HOME/bin/javac" -cp "$SVM_JAR" resources/CutOffMisc.java

if [ -z "$BABASHKA_JAR" ]; then
    lein with-profiles "$BABASHKA_LEIN_PROFILES,+reflection,-uberjar" do run
    lein with-profiles "$BABASHKA_LEIN_PROFILES" do clean, uberjar, metabom
fi
